package cq.zhcqk;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import com.dou361.dialogui.DialogUIUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.io.File;

import cq.zhcqk.service.X5CorePreLoadService;
import cq.zhcqk.util.Config;
import cq.zhcqk.util.NetworkUtil;


public class app extends Application{
    public static Context applicationContext;
    private static app instance;
    public static String currentUserNick = "";
    public static Boolean isLogin = false;
    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        instance = this;
        DialogUIUtils.init(applicationContext);
//        if(NetworkUtil.isNetworkAvailable(applicationContext)){
        initImageLoader();
        preInitX5Core();
        intShare();
//        }
    }

    public static app getInstance() {
        return instance;
    }

    public static Context getContext(){
        return applicationContext;
    }

    private void intShare(){
        UMShareAPI.get(this);
    }

    {
        PlatformConfig.setWeixin("wxfc3b0663adc00210","56f30F118Ddf84763E133bb97af852ff");
//        PlatformConfig.setQQZone("1106699198", "Ep4PHhZZRprklKP2");
        PlatformConfig.setQQZone("1106699583", "0jrdmUhSACywDDAg");
//        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
    }

    /**
     * 初始化X5内核
     */
    private void preInitX5Core() {
        //预加载x5内核
        Intent intent = new Intent(this, X5CorePreLoadService.class);
        startService(intent);
    }
    /**
     * 腾讯X5浏览器
     */
    private void X5(){
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(),  cb);
    }

    //初始化网络图片缓存库
    private void initImageLoader() {
        File imgFile = new File(Environment.getExternalStorageDirectory(), Config.imgPath);
        if(!imgFile.exists()){
            imgFile.mkdirs();
        }
        //网络图片例子,结合常用的图片缓存库UIL,你可以根据自己需求自己换其他网络图片库
//        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
////                .showImageForEmptyUri(R.drawable.ic_default_adimage)
//                .cacheInMemory(true).cacheOnDisk(true).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this.getApplicationContext())
//                .defaultDisplayImageOptions(defaultOptions)
                .threadPoolSize(3) // default  线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())

                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)  // 内存缓存的最大值
                .memoryCacheSizePercentage(13) // default

                .diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
                .discCacheFileCount(100) //缓存的文件数量
                .discCache(new UnlimitedDiskCache(imgFile))//自定义缓存路径
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
}
